package com.jse;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jse.ioc.Ioc;
import com.jse.log.Log;
import com.jse.log.Logs;


public class Jse {

	public static ServletContext sc;
	public static Ioc ioc;
	public static HttpServletRequest req;
	public static HttpServletResponse resp;
	public static Charset encoding=Charset.forName("UTF-8");
	public static final String UTF8 = "UTF-8";
	public static String DBTYPE = "mysql";
	
	static Log log=Logs.get();
	
	public static void main(String[] args) {
		log.info("xxxxx");
	}
	
	Map<String, String> getenv = System.getenv();
	
	public static void system() {
		log.info("Java的运行环境版本："+System.getProperty("java.version"));
	       log.info("Java的运行环境供应商："+System.getProperty("java.vendor"));
	       log.info("Java供应商的URL："+System.getProperty("java.vendor.url"));
	       log.info("Java的安装路径："+System.getProperty("java.home"));
	       log.info("Java的虚拟机规范版本："+System.getProperty("java.vm.specification.version"));
	       log.info("Java的虚拟机规范供应商："+System.getProperty("java.vm.specification.vendor"));
	       log.info("Java的虚拟机规范名称："+System.getProperty("java.vm.specification.name"));
	       log.info("Java的虚拟机实现版本："+System.getProperty("java.vm.version"));
	       log.info("Java的虚拟机实现供应商："+System.getProperty("java.vm.vendor"));
	       log.info("Java的虚拟机实现名称："+System.getProperty("java.vm.name"));
	       log.info("Java运行时环境规范版本："+System.getProperty("java.specification.version"));
	       log.info("Java运行时环境规范供应商："+System.getProperty("java.specification.vender"));
	       log.info("Java运行时环境规范名称："+System.getProperty("java.specification.name"));
	       log.info("Java的类格式版本号："+System.getProperty("java.class.version"));
	       log.info("Java的类路径："+System.getProperty("java.class.path"));
	       log.info("加载库时搜索的路径列表："+System.getProperty("java.library.path"));
	       log.info("默认的临时文件路径："+System.getProperty("java.io.tmpdir"));
	       log.info("一个或多个扩展目录的路径："+System.getProperty("java.ext.dirs"));
	       log.info("操作系统的名称："+System.getProperty("os.name"));
	       log.info("操作系统的构架："+System.getProperty("os.arch"));
	       log.info("操作系统的版本："+System.getProperty("os.version"));
	       log.info("文件分隔符："+System.getProperty("file.separator"));   //在 unix 系统中是＂／＂
	       log.info("路径分隔符："+System.getProperty("path.separator"));   //在 unix 系统中是＂:＂
	       log.info("行分隔符："+System.getProperty("line.separator"));   //在 unix 系统中是＂/n＂
	       log.info("用户的账户名称："+System.getProperty("user.name"));
	       log.info("用户的主目录："+System.getProperty("user.home"));
	       log.info("用户的当前工作目录："+System.getProperty("user.dir"));
	       log.info("IP地址:"+ip());
	       log.info("主机名称:"+host());
	       log.info("Mac地址:"+mac());
	       log.info("获取当前系统名称:"+os());
	}
	
	 private static Jse jse = null;
	    private static InetAddress localHost = null;

	    static{
	        try {
	            localHost = InetAddress.getLocalHost();
	        } catch (UnknownHostException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        }
	    }

	    /**
	     * 单例模式获取对象
	     * 
	     * @return
	     */
	    public static Jse getInstance() {
	        if (jse == null)
	            jse = new Jse();
	        return jse;
	    }

	    /**
	     * 本地IP
	     * 
	     * @return IP地址
	     */
	    public static String ip() {
	        String ip = localHost.getHostAddress();
	        return ip;
	    }

	    /**
	     * 获取用户机器名称
	     * 
	     * @return
	     */
	    public static String host() {
	        return localHost.getHostName();
	    }

	    /**
	     * 获取Mac地址
	     * 
	     * @return Mac地址，例如：F0-4D-A2-39-24-A6
	     */
	    public static String mac() {
	        NetworkInterface byInetAddress;
	        try {
	            byInetAddress = NetworkInterface.getByInetAddress(localHost);
	            byte[] hardwareAddress = byInetAddress.getHardwareAddress();
	            return getMacFromBytes(hardwareAddress);
	        } catch (SocketException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        }
	        return null;
	    }

	    /**
	     * 获取当前系统名称
	     * 
	     * @return 当前系统名，例如： windows xp
	     */
	    public static String os() {
	        Properties sysProperty = System.getProperties();
	        // 系统名称
	        String systemName = sysProperty.getProperty("os.name");
	        return systemName;
	    }
	    
	    public static boolean isWindow() {
	    	if (os().toLowerCase().startsWith("win")) {
				return true;
			}else {
				return false;
			}
	    }

	    private static String getMacFromBytes(byte[] bytes) {
	        StringBuffer mac = new StringBuffer();
	        byte currentByte;
	        boolean first = false;
	        for (byte b : bytes) {
	            if (first) {
	                mac.append("-");
	            }
	            currentByte = (byte) ((b & 240) >> 4);
	            mac.append(Integer.toHexString(currentByte));
	            currentByte = (byte) (b & 15);
	            mac.append(Integer.toHexString(currentByte));
	            first = true;
	        }
	        return mac.toString().toUpperCase();
	    }

		public static String version() {
			return "0.0.1";
		}

		public static void set(HttpServletRequest req, HttpServletResponse resp) {
			Jse.req=req;
			Jse.resp=resp;
		}

		public static HttpServletRequest getReq() {
			return req;
		}

		public static void setReq(HttpServletRequest req) {
			Jse.req = req;
		}

		public static HttpServletResponse getResp() {
			return resp;
		}

		public static void setResp(HttpServletResponse resp) {
			Jse.resp = resp;
		}

		public static ServletContext getSc() {
			return sc;
		}

		public static void setSc(ServletContext sc) {
			Jse.sc = sc;
		}

		public static Ioc getIoc() {
			return ioc;
		}

		public static void setIoc(Ioc ioc) {
			Jse.ioc = ioc;
		}
		
}
